﻿using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for ResourceManage
/// </summary>
public class ResourceManage
{
    public enum ResourceManagerType { stringText = 1, ErrorMessage = 0 };

    public ResourceManage()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static ForumMessage GetMessage(int nameH, String Language)
    {
        Hashtable resources = GetResource(ResourceManagerType.ErrorMessage, Language);
        try
        {
            if (resources[nameH] == null)
            {

                // LN 6/9/04: Changed to throw a forums exception 
                //  Throw New ForumException(ForumExceptionType.ResourceNotFound, "Value not found in Resources.xml for: " + name)
                // Throw New System.Exception("An exception has occurred.")
                return null;
            }
            else
            {
                return resources[nameH] as ForumMessage;
                // Return CStr(resources(nameH)).TrimEnd
                // Return Language & "/" & CStr(resources(nameH)).TrimEnd
            }
        }
        catch (Exception ex)
        {
            return null;
        }

    }

    public static String GetString(String nameH, string Language)
    {
        //        'If GetRigthPath(myPath.ToLower) Then
        //        'Else
        //        '    Language = "http://localhost/tYbay"
        //        'End If

        Hashtable resources = GetResource(ResourceManagerType.stringText, Language);
        try
        {
            if (resources[nameH] == string.Empty)
                // LN 6/9/04: Changed to throw a forums exception 
                //  Throw New ForumException(ForumExceptionType.ResourceNotFound, "Value not found in Resources.xml for: " + name)
                // Throw New System.Exception("An exception has occurred.")
                return string.Empty;
            else
                return resources[nameH].ToString().TrimEnd();
            //                ' Return Language & "/" & CStr(resources(nameH)).TrimEnd
        }
        catch (Exception ex)
        {
            return string.Empty;
        }
    }


    private static void LoadResource(ResourceManagerType resourceType, Hashtable target, String language, String cacheKey)
    {
        // ByVal myPath As String
        HttpContext MyContext = HttpContext.Current;
        String filePath=string.Empty;    // folder

        switch (resourceType)
        {
            case ResourceManagerType.ErrorMessage:
                filePath = MyContext.Server.MapPath("../XmlResource/XMLMailFile.xml");
                break;
            default:
                filePath = MyContext.Server.MapPath("../XmlResource/Resources.xml");
                break;
        }

        System.Web.Caching.CacheDependency dp = new System.Web.Caching.CacheDependency(filePath);

        System.Xml.XmlDocument d = new System.Xml.XmlDocument();
        try
        {
            d.Load(filePath);
        }
        catch (Exception ex)
        {
            return;
        }

        foreach (System.Xml.XmlNode n in d.SelectSingleNode("root").ChildNodes)
        {
                        if(n.NodeType!= System.Xml.XmlNodeType.Comment)
            //                '  Response.Write("name=" & n.Attributes("name").Value & "Value=" & n.InnerText & " <br> -****** ******<br>")
            //                '  target.Add(n.Attributes("name").Value, n.InnerText)

                            switch (resourceType)
                            {
                                case ResourceManagerType.ErrorMessage:
                                    ForumMessage m = new ForumMessage(n);
                                        target[m.messageID] = m;
                                    break;

                                case ResourceManagerType.stringText:
                                    if (target[n.Attributes["name"].Value] == null)
                                    {
                                        target.Add(n.Attributes["name"].Value, n.InnerText);
                                    }
                                    else
                                    {
                                       target[n.Attributes["name"].Value] = n.InnerText;
                                    }
                                    break;
                            }
            //            ' Response.Write(n.NodeType & " n.NodeType  System.Xml.XmlNodeType.Comment" & System.Xml.XmlNodeType.Comment & " <br>")
            //            '   Response.Write(n.Attributes("name").Value & "<br>")

        }

        DateTime cacheUntil;
        if (language == "ru")
            cacheUntil = DateTime.MaxValue;
        else
            cacheUntil = DateTime.Now.AddHours(1);

        MyContext.Cache.Insert(cacheKey, target, dp, cacheUntil, System.TimeSpan.Zero);
    }

    private static Hashtable GetResource(ResourceManagerType resourceType, String Language)
    {
        // ByVal myPath As String
        HttpContext context = HttpContext.Current;
        Hashtable resources;

        String defaultLanguage = "ru";// 'ForumConfiguration.GetConfig().DefaultLanguage;
        //        'Dim  = "heb" 'Users.GetUser().Language;
        String cacheKey = resourceType.ToString() + defaultLanguage + Language;

        //Ensure the user has a language set

        if (Language == string.Empty)
        {
            Language = defaultLanguage;
        }
        //Attempt to get the resources from the Cache
        //Try


        if (context.Cache[cacheKey] == null)
        {
            resources = new Hashtable();
            // First load the English resouce, changed from loading the default language
            // since the userLanguage is set to the defaultLanguage if the userLanguage
            // is unassigned. We load the english language always just to ensure we have
            // a resource loaded just incase the userLanguage doesn't have a translated
            // string for this English resource.

            //LoadResource(resourceType, resources, "heb", cacheKey)

            //            '// If the user language is different load it

            //            ' If ("heb" <> Language) Then
            LoadResource(resourceType, resources, Language, cacheKey);
            //            '  LoadResource(resourceType, resources, Language, cacheKey, myPath)
            //            'End If

        }
        return context.Cache[cacheKey] as Hashtable;

        //        '  Catch ex As Exception
        //        '     Return Nothing
        //        '  End Try
    }
    //    'Shared Function GetRigthPath(ByVal Url As String) As Boolean
    //    '    Dim ok As Boolean = False
    //    '    Dim myUrl As String = Url.ToString.ToLower

    //    '    If myUrl.IndexOf("localhost") = -1 Then
    //    '        Return True
    //    '    Else
    //    '        Return False
    //    '    End If
    //    'End Function
    //End Class
}
